package admin

import (
	"chaowan/data_source"
	"chaowan/models"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

type AdminloginBody struct {
	Account  string `json:"account" from:"account"`
	Password string `json:"password" from:"password"`
}

func Adminlogin(ctx *gin.Context) {
	request := AdminloginBody{}
	ctx.ShouldBind(&request)
	username := request.Account
	pwd := request.Password
	var admin models.Adminuser
	data_source.Db.Where("username = ?", username).First(&admin)
	if admin.ID == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "账号不存在",
		})
		return
	}
	if admin.Pwd != pwd {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "密码错误",
		})
		return
	}
	var quanxian models.Quanxian
	data_source.Db.Where("id = ?", admin.Quanxianid).First(&quanxian)
	admin.Quanxian = quanxian

	reqip := ctx.ClientIP()
	adminlog := models.Adminlog{
		Adminuserid: admin.ID,
		Addtime:     time.Now().Unix(),
		Content:     "登录成功",
		Ip:          reqip,
	}
	data_source.Db.Create(&adminlog)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    admin,
		"msg":     "ok",
	})
	return
}

func Quanxianlist(ctx *gin.Context) {
	var quanxian []models.Quanxian
	data_source.Db.Find(&quanxian)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    quanxian,
		"msg":     "ok",
	})
	return

}

type QuanxianBody struct {
	ID      int64  `json:"id" from:"id"`
	Name    string `json:"name" form:"name"`
	Content string `json:"content" form:"content"`
}

func Quanxianadd(ctx *gin.Context) {
	request := QuanxianBody{}
	ctx.ShouldBind(&request)

	name := request.Name
	content := request.Content

	banner := models.Quanxian{
		Name:    name,
		Content: content,
	}
	data_source.Db.Create(&banner)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Quanxianupdate(ctx *gin.Context) {
	request := QuanxianBody{}
	ctx.ShouldBind(&request)

	id := request.ID
	name := request.Name
	content := request.Content

	banner := models.Quanxian{
		Name:    name,
		Content: content,
	}
	data_source.Db.Where("id = ?", id).Updates(&banner)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Quanxiandel(ctx *gin.Context) {
	request := QuanxianBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	data_source.Db.Where("id = ?", id).Delete(&models.Quanxian{})
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Adminlist(ctx *gin.Context) {
	var admin []models.Adminusersql
	data_source.Db.Model(&models.Adminuser{}).Select("adminuser.*, quanxian.name as quanxianname").Joins("left join quanxian on quanxian.id = adminuser.quanxianid").Find(&admin)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    admin,
		"msg":     "ok",
	})
	return

}

type AdminBody struct {
	ID         int64  `json:"id" from:"id"`
	Name       string `orm:"column(name)" json:"name"`
	Username   string `orm:"column(username)" json:"username"`
	Pwd        string `orm:"column(pwd)" json:"pwd"`
	Pwdnew     string `orm:"column(pwdnew)" json:"pwdnew"`
	Quanxianid int64  `json:"quanxianid" form:"quanxianid"`
}

func Adminadd(ctx *gin.Context) {
	request := AdminBody{}
	ctx.ShouldBind(&request)

	name := request.Name
	username := request.Username
	pwd := request.Pwd
	quanxianid := request.Quanxianid

	admin := models.Adminuser{
		Name:       name,
		Username:   username,
		Pwd:        pwd,
		Quanxianid: quanxianid,
	}
	data_source.Db.Create(&admin)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Adminupdate(ctx *gin.Context) {
	request := AdminBody{}
	ctx.ShouldBind(&request)

	id := request.ID
	name := request.Name
	username := request.Username
	pwd := request.Pwd
	quanxianid := request.Quanxianid

	admin := models.Adminuser{
		Name:       name,
		Username:   username,
		Pwd:        pwd,
		Quanxianid: quanxianid,
	}
	data_source.Db.Where("id = ?", id).Updates(&admin)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Adminupdatepwd(ctx *gin.Context) {
	request := AdminBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	pwd := request.Pwd
	pwdnew := request.Pwdnew
	var user models.Adminuser
	data_source.Db.Where("id = ?", id).First(&user)
	if user.Pwd != pwd {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "原密码错误",
		})
		return
	}
	data_source.Db.Model(&models.Adminuser{}).Where("id = ?", id).Update("pwd", pwdnew)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Admindel(ctx *gin.Context) {
	request := AdminBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	data_source.Db.Where("id = ?", id).Delete(&models.Adminuser{})
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
